/**
 * 
 * 新增时间：2010-05-05
 * 新增人：朱家进
 * 新增内容：提交上传文件内容，生成组织和用户
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;
import com.zjht.gmcc.bean.util.MySession;

public class OrgaBatchUploadSubmit extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		ArrayList trueLineList = (ArrayList) session.getAttribute("trueLineList");
		Iterator it = trueLineList.iterator();
		String[] line = null;
		String organame = null;
		String orgacode = null;
		String mobileorgacode = null;
		String countryorgacode = null;
		String provorgacode = null;
		String halltime = null;
		String halltype = null;
		String hour = null;
		String money = null;
		String jmpassword = MyConstant.INIT_PWD;
		int flag = 0;
		String sql = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		String areaid = String.valueOf(MySession.getAreaid(session));
		String areaShortName = null;
		String paraid = null;
		String opercode = MySession.getUsercode(session);

		String areaidSql = "SELECT shortname FROM area WHERE areaid = ?";
		PreparedStatement areaidPs = myJDBC.prepareStatement(areaidSql);
		areaidPs.setString(1, areaid);
		ResultSet areaidRs = areaidPs.executeQuery();
		if (areaidRs.next()) {
			areaShortName = areaidRs.getString("shortname");
		}

		String paramSql = "SELECT paraid FROM parameter WHERE paraname = ?";
		PreparedStatement paramPs = myJDBC.prepareStatement(paramSql);

		String provSql = "INSERT INTO organiza(orgacode,organame,parentorgacode,orgatypeid,"
				+ "actorcode,areaid,createdate,shortname,paraid,channeltype,status,orgid,parentorgid) " + "VALUES(?,?,?,?,?,?,sysdate,?,?,?,?,?,-1)";
		PreparedStatement provPs = myJDBC.prepareStatement(provSql);

		String citySql = "INSERT INTO organiza(orgacode,organame,parentorgacode,orgatypeid,"
				+ "actorcode,areaid,createdate,shortname,paraid,channeltype,status,orgid,parentorgid) " + "VALUES(?,?,?,?,?,?,sysdate,?,?,?,?,?,?)";
		PreparedStatement cityPs = myJDBC.prepareStatement(citySql);

		String hallSql = "INSERT INTO organiza(orgacode,organame,parentorgacode,orgatypeid,"
				+ "actorcode,areaid,createdate,shortname,paraid,channeltype,sdate,edate,fillhour,status,orgid,parentorgid) "
				+ "VALUES(?,?,?,?,?,?,sysdate,?,?,?,?,?,?,?,?,?)";
		PreparedStatement hallPs = myJDBC.prepareStatement(hallSql);
		String providerSql = "INSERT INTO mobiletoprovider(mobileorgacode,providerorgacode,cityproviderorgacode,status,validdate,cityprovidertype)"
				+ "VALUES(?,?,?,?,sysdate,?)";
		PreparedStatement providerPs = myJDBC.prepareStatement(providerSql);

		String bossSql = "INSERT INTO mobileorgatolocal(mobileorgacode,orgacode)VALUES(?,?)";
		PreparedStatement bossPs = myJDBC.prepareStatement(bossSql);

		String priceSql = "INSERT INTO SS_PRICE (ID,OrgaCode,Price,CreateDate,CreateOperCode) VALUES (SS_PriceID.NEXTVAL,?,?,SYSDATE,?)";
		PreparedStatement pricePs = myJDBC.prepareStatement(priceSql);

		String opersDutySql = "INSERT INTO OPERSPOPEDOM (opercode,dutyid,operid) VALUES (?,?,?)";
		PreparedStatement operDutyPs = myJDBC.prepareStatement(opersDutySql);

		String opersSql = "INSERT INTO opers(opercode, opername, passwd, orgacode, deptid,createdate, status, opertype, showflag,orgid,operid) "
				+ "VALUES(?,?,?,?,?,sysdate,1,1,1,?,?)";
		PreparedStatement operPs = myJDBC.prepareStatement(opersSql);

		String provOrgacode = null;// 省仓编码
		String cityOrgacode = null;// 市仓编码
		String hallOrgacode = null;// 厅编码
		String provJL = null;// 省仓经理
		String provCG = null;// 省仓仓管
		String cityJL = null;// 市仓经理
		String cityCG = null;// 市仓仓管
		String hallDZ = null;// 厅店长
		String hallCG = null;// 厅仓管
		String provOrgid = null;
		String cityOrgid = null;
		String hallOrgid = null;
		String countryOrgid = null;
		String operid = null;
		while (it.hasNext()) {
			line = it.next().toString().split("\\|");
			organame = line[0];
			orgacode = line[1].toUpperCase();
			mobileorgacode = line[2].toUpperCase();
			countryorgacode = line[3].toUpperCase();
			// cityorgacode = line[4].toUpperCase();
			provorgacode = line[5].toUpperCase();
			halltime = line[6];
			halltype = line[7];
			hour = line[8];
			money = line[9];

			paramPs.setString(1, halltype);
			ResultSet paramRs = paramPs.executeQuery();
			if (paramRs.next()) {
				paraid = paramRs.getString("paraid");
			}

			provOrgacode = areaShortName.toUpperCase() + "A" + orgacode;// 省仓编码
			cityOrgacode = areaShortName.toUpperCase() + "B" + orgacode;// 市仓编码
			hallOrgacode = areaShortName.toUpperCase() + "C" + orgacode + "001";// 厅编码
			provJL = areaShortName.toUpperCase() + "A" + orgacode + "1";// 省仓经理
			provCG = areaShortName.toUpperCase() + "A" + orgacode + "2";// 省仓仓管
			cityJL = areaShortName.toUpperCase() + "B" + orgacode + "1";// 市仓经理
			cityCG = areaShortName.toUpperCase() + "B" + orgacode + "2";// 市仓仓管
			hallDZ = areaShortName.toUpperCase() + "C" + orgacode + "001" + "1";// 厅店长
			hallCG = areaShortName.toUpperCase() + "C" + orgacode + "001" + "2";// 厅仓管

			provOrgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));
			cityOrgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));
			hallOrgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));
			if (provorgacode.equals("无")) {// 省仓
				provPs.setString(1, provOrgacode);
				provPs.setString(2, organame);
				provPs.setString(3, "0");
				provPs.setString(4, "5");
				provPs.setString(5, "P");
				provPs.setString(6, areaid);
				provPs.setString(7, organame);
				provPs.setString(8, "0");
				provPs.setString(9, MyConstant.CHANNEL_SOCIAL_CITY);
				provPs.setString(10, "1");
				provPs.setString(11, provOrgid);
				flag = provPs.executeUpdate();
			}
			if (flag == 1) {
				if (provorgacode.equals("无")) {// 市仓
					cityPs.setString(1, cityOrgacode);
					cityPs.setString(2, organame);
					cityPs.setString(3, provOrgacode);
					cityPs.setString(4, "6");
					cityPs.setString(5, "P");
					cityPs.setString(6, areaid);
					cityPs.setString(7, organame);
					cityPs.setString(8, "0");
					cityPs.setString(9, MyConstant.CHANNEL_SOCIAL_CITY);
					cityPs.setString(10, "1");
					cityPs.setString(11, cityOrgid);
					cityPs.setString(12, provOrgid);
					flag = cityPs.executeUpdate();
				}
			}
			sql = "SELECT orgid FROM organiza WHERE orgacode = ? ";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, countryorgacode);
			rs = ps.executeQuery();
			countryOrgid = null;
			if (rs.next()) {
				countryOrgid = rs.getString(1);
			}
			rs.close();
			if (flag == 1) {
				hallPs.setString(1, hallOrgacode);
				hallPs.setString(2, organame);
				hallPs.setString(3, countryorgacode);
				hallPs.setString(4, "4");
				hallPs.setString(5, "M");
				hallPs.setString(6, areaid);
				hallPs.setString(7, organame);
				hallPs.setString(8, paraid);
				hallPs.setString(9, MyConstant.CHANNEL_SOCIAL_CITY);
				hallPs.setString(10, hour.split("-")[0]);
				hallPs.setString(11, hour.split("-")[1]);
				hallPs.setString(12, halltime);
				hallPs.setString(13, "1");
				hallPs.setString(14, hallOrgid);
				hallPs.setString(15, countryOrgid);
				flag = hallPs.executeUpdate();
			}
			if (flag == 1) {
				providerPs.setString(1, hallOrgacode);
				providerPs.setString(2, provOrgacode);
				providerPs.setString(3, cityOrgacode);
				providerPs.setString(4, "1");
				providerPs.setString(5, "1");
				flag = providerPs.executeUpdate();
			}
			if (flag == 1) {
				bossPs.setString(1, mobileorgacode);
				bossPs.setString(2, hallOrgacode);
				flag = bossPs.executeUpdate();
			}
			if (flag == 1) {
				pricePs.setString(1, hallOrgacode);
				pricePs.setString(2, money);
				pricePs.setString(3, opercode);
				flag = pricePs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, provJL);
				operPs.setString(2, "省仓经理");
				operPs.setString(3, jmpassword);
				operPs.setString(4, provOrgacode);
				operPs.setString(5, "50");
				operPs.setString(6, provOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, provJL);
				operDutyPs.setString(2, "5000");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, provCG);
				operPs.setString(2, "省仓仓管");
				operPs.setString(3, jmpassword);
				operPs.setString(4, provOrgacode);
				operPs.setString(5, "53");
				operPs.setString(6, provOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, provCG);
				operDutyPs.setString(2, "5300");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, cityJL);
				operPs.setString(2, "市仓经理");
				operPs.setString(3, jmpassword);
				operPs.setString(4, cityOrgacode);
				operPs.setString(5, "60");
				operPs.setString(6, cityOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, cityJL);
				operDutyPs.setString(2, "6000");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, cityCG);
				operPs.setString(2, "市仓仓管");
				operPs.setString(3, jmpassword);
				operPs.setString(4, cityOrgacode);
				operPs.setString(5, "63");
				operPs.setString(6, cityOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, cityCG);
				operDutyPs.setString(2, "6300");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, hallDZ);
				operPs.setString(2, "店长");
				operPs.setString(3, jmpassword);
				operPs.setString(4, hallOrgacode);
				operPs.setString(5, "40");
				operPs.setString(6, hallOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, hallDZ);
				operDutyPs.setString(2, "4000");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}

			operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
			if (flag == 1) {
				operPs.setString(1, hallCG);
				operPs.setString(2, "厅仓管");
				operPs.setString(3, jmpassword);
				operPs.setString(4, hallOrgacode);
				operPs.setString(5, "42");
				operPs.setString(6, hallOrgid);
				operPs.setString(7, operid);
				flag = operPs.executeUpdate();
			}
			if (flag == 1) {
				operDutyPs.setString(1, hallCG);
				operDutyPs.setString(2, "4200");
				operDutyPs.setString(3, operid);
				flag = operDutyPs.executeUpdate();
			}
			if (flag == 1)// 当新增服务厅时，如本市有a.multiplat = '1'平台，则把对应关系加到mobiletoprovider
			{
				sql = "SELECT a.orgacode,a.parentorgacode FROM organiza a "
						+ "WHERE a.multiplat = '1' AND a.areaid = ? AND a.orgatypeid = 6 AND a.actorcode = 'P' AND a.status = '1'";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, areaid);
				rs = ps.executeQuery();

				sql = "INSERT INTO mobiletoprovider(mobileorgacode, providerorgacode, cityproviderorgacode, status, "
						+ "ValidDate, CityProviderType,multiplat) " + "VALUES(?, ?, ?, 1, Sysdate, '1','1')";
				ps = myJDBC.prepareStatement(sql);

				while (rs.next()) {
					ps.setString(1, hallOrgacode);
					ps.setString(2, rs.getString("parentorgacode"));
					ps.setString(3, rs.getString("orgacode"));
					flag = ps.executeUpdate();
					if (flag == 0)
						break;
				}
				ps.close();
				ps = null;
			}
			if (flag == 0)
				break;
		}

		if (flag == 1) {
			InitParam.initOrgaInfo();
			alert(out, "市级社会渠道批量导入成功", "/sys/orga/orgabatch_citysociety.jsp");
		} else {

			errAlert(request, out, menu, "市级社会渠道批量导入失败", "/sys/orga/orgabatch_citysociety_view.jsp");
		}

		return flag == 1;

	}
}
